package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.joker.common.core.domain.entity.SysUser;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import com.joker.orange.service.IInviteCodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.UserInfoBo;
import com.joker.orange.domain.vo.UserInfoVo;
import com.joker.orange.domain.UserInfo;
import com.joker.orange.mapper.UserInfoMapper;
import com.joker.orange.service.IUserInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Random;

/**
 * 用户信息Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class UserInfoServiceImpl implements IUserInfoService {

    private final UserInfoMapper baseMapper;
    private final IInviteCodeService inviteCodeService;

    @Override
    public UserInfo findUserInfoById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询用户信息
     */
    @Override
    public UserInfoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public UserInfoVo queryUserInfo(UserInfoBo bo) {
        LambdaQueryWrapper<UserInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public TableDataInfo<UserInfoVo> queryPageList(UserInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserInfo> lqw = buildQueryWrapper(bo);
        Page<UserInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public List<UserInfoVo> queryList(UserInfoBo bo) {
        LambdaQueryWrapper<UserInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserInfo> buildQueryWrapper(UserInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), UserInfo::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getAccount()), UserInfo::getAccount, bo.getAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), UserInfo::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), UserInfo::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), UserInfo::getAvatar, bo.getAvatar());
        lqw.eq(bo.getDiamond() != null, UserInfo::getDiamond, bo.getDiamond());
        lqw.eq(StringUtils.isNotBlank(bo.getIsMember()), UserInfo::getIsMember, bo.getIsMember());
        lqw.eq(StringUtils.isNotBlank(bo.getIsGridMember()), UserInfo::getIsGridMember, bo.getIsGridMember());
        lqw.eq(StringUtils.isNotBlank(bo.getMemberLevel()), UserInfo::getMemberLevel, bo.getMemberLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getMemberExpireTime()), UserInfo::getMemberExpireTime, bo.getMemberExpireTime());
        lqw.eq(StringUtils.isNotBlank(bo.getGridMemberLevel()), UserInfo::getGridMemberLevel, bo.getGridMemberLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getGridMemberExpireTime()), UserInfo::getGridMemberExpireTime, bo.getGridMemberExpireTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSpreadId()), UserInfo::getSpreadId, bo.getSpreadId());
        lqw.eq(StringUtils.isNotBlank(bo.getShortVideoCount()), UserInfo::getShortVideoCount, bo.getShortVideoCount());
        lqw.eq(StringUtils.isNotBlank(bo.getLongVideoCount()), UserInfo::getLongVideoCount, bo.getLongVideoCount());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), UserInfo::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()), UserInfo::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, UserInfo::getLoginDate, bo.getLoginDate());
        return lqw;
    }

    /**
     * 新增用户信息
     */
    @Override
    public Boolean insertByBo(UserInfoBo bo) {
        UserInfo add = BeanUtil.toBean(bo, UserInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertUserInfo(UserInfo bo) {
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateUserInfo(UserInfo userInfo) {
        return baseMapper.updateById(userInfo) > 0;
    }

    /**
     * 修改用户信息
     */
    @Override
    public Boolean updateByBo(UserInfoBo bo) {
        UserInfo update = BeanUtil.toBean(bo, UserInfo.class);
        if ("1".equals(update.getSuperiorType())) {
            update.setSpreadId("1");
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public UserInfo querySuperior(String inviteCode) {
        return baseMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
            .eq(UserInfo::getInviteCode, inviteCode));
    }

    @Override
    public UserInfo queryUserByDeviceId(String deviceId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
            .eq(UserInfo::getDeviceId, deviceId));
    }

    /**
     * 生成6位随机的邀请码
     */
    private String getInviteCode() {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder(6);
        for (int i = 0; i < 6; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }
}
